<template>
  <div class="app-container">
    <el-row v-permission="['system:dictType:query']">
      <!-- 字典类型管理 -->
      <!-- span 12 就是页面的一半 -->
      <el-col :span="12">
        <el-card title="dictDataCardTitle">
          <!-- 字典类型搜索表单 -->
          <el-form :inline="true" size="small" :model="page.params">
            <el-form-item>
              <el-input v-model="page.params.dictName" placeholder="字典名称"/>
            </el-form-item>
            <el-form-item>
              <el-select clearable v-model="page.params.status" placeholder="请选择状态">
                <el-option v-for="item in statusOptions"
                           :key="item.dictValue"
                           :label="item.dictLabel"
                           :value="item.dictValue"/>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button
                type="primary"
                icon="el-icon-search"
                size="small"
                @click="search">查询
              </el-button>
            </el-form-item>
            <el-form-item>
              <el-button
                type="warning"
                icon="el-icon-refresh"
                size="small"
                @click="resetSearch">重置
              </el-button>
            </el-form-item>
            <el-form-item>
              <el-button v-permission="['system:dictType:add']"
                         type="success"
                         icon="el-icon-plus"
                         size="small"
                         @click="toAddDictType">添加字典类型
              </el-button>
            </el-form-item>
          </el-form>
          <!-- 字典类型数据表格 -->
          <el-table
            v-loading="loading"
            :data="pageData.list"
            border
            stripe
            @row-click="dictTypeRowClick"
            style="width: 100%"
          >
            <el-table-column prop="dictId" label="类型编号" align="center"/>
            <el-table-column prop="dictName" label="类型名称" align="center"/>
            <el-table-column prop="dictType" label="字典类型" align="center"/>
            <el-table-column prop="remark" label="类型备注" align="center"/>
            <el-table-column prop="status" label="状态" align="center">
              <template slot-scope="{row}">
                <dict-tag :data-list="statusOptions" :value="row.status"/>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center">
              <template slot-scope="{row}">
                <el-dropdown>
                  <span class="el-dropdown-link">
                  <el-link icon="el-icon-s-operation" type="primary">操作
                  <i class="el-icon-arrow-down el-icon--right"/>
                  </el-link>
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item>
                        <el-button v-permission="['system:dictType:update']"
                                   type="text" icon="el-icon-edit" @click="toUpdateDictType(row)">修改</el-button>
                      </el-dropdown-item>
                      <el-dropdown-item>
                        <el-button v-permission="['system:dictType:remove']"
                                   type="text" icon="el-icon-delete" @click="toDeleteDictType(row)">删除</el-button>
                      </el-dropdown-item>
                    </el-dropdown-menu>
                </span>
                </el-dropdown>
              </template>
            </el-table-column>
          </el-table>
          <!-- 字典类型分页 -->
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="page.pageNumber"
            :page-sizes="[5,10,15,20,25]"
            :page-size="page.pageSize"
            layout="total,sizes,prev,pager,next,jumper"
            :total="pageData.totalCount"
            align="center"
          />
        </el-card>
      </el-col>
      <!-- 字典数据管理 -->
      <el-col :span="12" v-permission="['system:dictData:query']">
        <el-card :header=dictDataCardTitle>
          <!-- 字典数据操作 -->
          <el-form>
            <el-form-item>
              <el-button v-permission="['system:dictData:add']"
                         type="success" icon="el-icon-plus" size="small"
                         :disabled="currentDictType===null" @click="toAddDictData">
                添加字典数据
              </el-button>
              <el-button v-permission="['system:dictType:cache']"
                         type="primary" icon="el-icon-refresh" size="small" @click="refreshCache">
                同步缓存
              </el-button>
            </el-form-item>
          </el-form>

          <!-- 字典数据表格 -->
          <el-table
            v-loading="dictDataLoading"
            :data="dictDataList"
            border
            stripe
            @row-click="dictTypeRowClick"
            style="width: 100%"
          >
            <el-table-column prop="dictCode" label="数据编号" align="center"/>
            <el-table-column prop="dictLabel" label="字典标签" align="center"/>
            <el-table-column prop="dictValue" label="字典键值" align="center"/>
            <el-table-column prop="dictSort" label="字典排序" align="center"/>
            <el-table-column prop="status" label="状态" align="center">
              <template slot-scope="{row}">
                <dict-tag :data-list="statusOptions" :value="row.status"/>
              </template>
            </el-table-column>
            <el-table-column prop="remark" label="备注" align="center"/>
            <el-table-column label="操作" align="center">
              <template slot-scope="{row}">
                <el-dropdown>
                  <span class="el-dropdown-link">
                  <el-link icon="el-icon-s-operation" type="primary">操作
                  <i class="el-icon-arrow-down el-icon--right"/>
                  </el-link>
                    <el-dropdown-menu slot="dropdown">
                     <el-dropdown-item>
                        <el-button v-permission="['system:dictData:update']"
                                   type="text" icon="el-icon-edit" @click="toUpdateDictData(row)">修改</el-button>
                      </el-dropdown-item>
                      <el-dropdown-item>
                        <el-button v-permission="['system:dictData:remove']"
                                   type="text" icon="el-icon-delete" @click="toDeleteDictData(row)">删除</el-button>
                      </el-dropdown-item>
                    </el-dropdown-menu>
                </span>
                </el-dropdown>
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-col>
    </el-row>
    <!-- 字典类型添加弹出框 -->
    <el-dialog :title="title"
               center
               width="50%"
               :visible.sync="addDictTypeDialog"
               v-if="addDictTypeDialog">
      <sys-dict-type-add @after="getDictTypeByPage" @close="closeDialog"/>
    </el-dialog>
    <!-- 字典类型修改弹出框 -->
    <el-dialog :title="title"
               center
               width="50%"
               :visible.sync="updateDictTypeDialog"
               v-if="updateDictTypeDialog">
      <sys-dict-type-update :active-id="dictTypeActiveId" @after="getDictTypeByPage" @close="closeDialog"/>
    </el-dialog>
    <!-- 字典数据添加的弹出框 -->
    <el-dialog :title="title"
               center
               width="50%"
               :visible.sync="addDictDataDialog"
               v-if="addDictDataDialog">
      <sys-dict-data-add :active-type="currentDictType.dictType"
                         @after="queryDictDataByDictType"
                         @close="closeDialog"/>
    </el-dialog>
    <!-- 字典数据修改的弹出框 -->
    <el-dialog
      :title="title"
      center
      width="50%"
      :visible.sync="updateDictDataDialog"
      v-if="updateDictDataDialog">
      <sys-dict-data-update
        :active-id="dictDataActiveId"
        @after="queryDictDataByDictType"
        @close="closeDialog"/>
    </el-dialog>
  </div>
</template>

<script>
import sysDictTypeApi from "@/api/system/sysDictType";
import sysDictDataApi from "@/api/system/sysDictData";
import sysDictTypeAdd from "./sys-dict-type-add";
import sysDictTypeUpdate from "./sys-dict-type-update";
import sysDictDataAdd from "./sys-dict-data-add";
import sysDictDataUpdate from "./sys-dict-data-update";


export default {
  name: "sys-dict-type-list",
  components: {
    sysDictTypeUpdate,
    sysDictTypeAdd,
    sysDictDataAdd,
    sysDictDataUpdate,
  },
  data() {
    return {
      // 字典类型加载中的进度条
      loading: false,
      // 字典数据加载中的进度条
      dictDataLoading: false,
      // 查询条件
      page: {
        pageNumber: 1,
        pageSize: 10,
        params: {
          dictName: undefined,
          status: undefined
        }
      },
      // 字典类型的表格数据
      pageData: {
        list: [],
        totalCount: 0
      },
      // 弹出框的标题
      title: "",
      // 是否打开添加字典类型的弹出框
      addDictTypeDialog: false,
      // 是否打开修改字典类型的弹出框
      updateDictTypeDialog: false,
      // 是否打开添加字典数据的弹出框
      addDictDataDialog: false,
      // 是否打开修改字典数据的弹出框
      updateDictDataDialog: false,
      // 当前选中的 activeId
      dictTypeActiveId: null,
      //记录当前左边点击的字典类型
      currentDictType: null,
      //当前选中字典数据的 activeId
      dictDataActiveId: null,
      // 字典数据的数组
      dictDataList: [],
      // 存放动态获取的字典数据
      statusOptions: [],
      // 字典数据管理卡片标题
      dictDataCardTitle: null,
    }
  },
  created() {
    // 分页条件查询字典类型
    this.getDictTypeByPage()
    // 加载字典数据
    this.getDictDataByDictType()
  },
  methods: {
    // 根据字典类型加载数据状态的字典数据
    getDictDataByDictType() {
      sysDictDataApi.getDictDataByDictType("sys_status_type").then(res => {
        this.statusOptions = res.data
      })
    },

    // 字典类型搜索
    search() {
      this.getDictTypeByPage()
      this.dictDataList = []
    },

    // 重置字典类型搜索
    resetSearch() {
      this.page.params = {}
      this.getDictTypeByPage()
    },

    // 分页查询字典类型
    getDictTypeByPage() {
      this.loading = true;
      sysDictTypeApi.getByPage(this.page).then(res => {
        this.pageData.list = res.data.list;
        this.pageData.totalCount = res.data.totalCount
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },

    // 分页大小发生变化时触发
    handleSizeChange(val) {
      this.page.pageSize = val
      this.getDictTypeByPage()
    },

    // 当前页发生变化时触发
    handleCurrentChange(val) {
      this.page.pageNumber = val
      this.getDictTypeByPage()
    },

    // 打开添加字典类型弹出框
    toAddDictType() {
      this.addDictTypeDialog = true;
      this.title = "添加字典类型"
    },

    // 点击了修改字典类型按钮
    toUpdateDictType(row) {
      // TODO 修改了字典类型后右边对应的字典数据还是修改前的，需要进行处理
      this.updateDictTypeDialog = true;
      this.title = "修改 " + row.dictName + " 字典类型"
      this.dictTypeActiveId = row.dictId;
    },
    // 点击了删除字典类型按钮
    toDeleteDictType(row) {
      this.$confirm(`此操作会删除 ${row.dictType} 字典类型，与之关联的字典数据也会全部删除，是否继续？`, '提示', {
        confirmButtonClass: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        sysDictTypeApi.deleteById(row.dictId).then(res => {
          this.$message.success('删除成功');
          this.getDictTypeByPage();
        })
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    },
    // 打开添加字典数据弹出框
    toAddDictData() {
      this.title = `为 ${this.currentDictType.dictName} 添加字典数据`
      this.addDictDataDialog = true;
    },
    //打开修改字典数据的弹出层
    toUpdateDictData(row) {
      this.updateDictDataDialog = true;
      this.title = "修改 " + row.dictLabel + " 字典数据"
      this.dictDataActiveId = row.dictCode;
    },
    //删除字典数据
    toDeleteDictData(row) {
      this.$confirm(`此操作会删除 ${row.dictLabel} 字典数据, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        sysDictDataApi.deleteById(row.dictCode).then(res => {
          this.$message.success('删除成功');
          this.queryDictDataByDictType();
        })
      }).catch(() => {
        this.$message.info('已取消删除');
      });
    },

    // 点击了字典类型中的某一列 -> 查询相应字典数据
    dictTypeRowClick(row) {
      this.dictDataCardTitle = "字典类型：" + row.dictType
      this.currentDictType = row
      this.dictDataLoading = true
      console.log(row.dictType)
      sysDictDataApi.getDictDataByDictType(row.dictType).then(res => {
        this.dictDataList = res.data
        this.dictDataLoading = false
      }).catch(() => {
        this.dictDataLoading = false
      })
    },
    // 刷新右边的数据
    queryDictDataByDictType() {
      sysDictDataApi.getDictDataByDictType(this.currentDictType.dictType).then(res => {
        this.dictDataList = res.data;
        this.dictDataLoading = false;
      }).catch(() => {
        this.dictDataLoading = false;
      })
    },

    // 同步缓存
    refreshCache() {
      this.loading = true
      this.dictDataLoading = true
      sysDictTypeApi.refreshCache().then(res => {
        this.$message.success(res.msg)
        this.loading = false
        if (this.currentDictType !== null) {
          this.queryDictDataByDictType()
        }
        this.dictDataLoading = false
      }).catch(() => {
        this.$message.error("同步失败")
        this.loading = false
      })
    },
    // 关闭所有弹出框
    closeDialog() {
      this.addDictTypeDialog = false;
      this.updateDictTypeDialog = false;
      this.addDictDataDialog = false;
      this.updateDictDataDialog = false;
    }
  }
}
</script>

<style scoped>

</style>
